package com.shop.service.impl;

import com.shop.bean.Product;
import com.shop.bean.User;
import com.shop.dao.UserDao;
import com.shop.service.IUserService;
import com.shop.util.LayData;
import com.shop.util.Page;
import com.shop.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    UserDao userDao;
    @Override
    public User getUserByID(Integer id) {
        return userDao.getUserByID(id);
    }

    public int editUser(User user){
        return userDao.editUser(user);
    }

    @Override
    public User getUser(User user) {
        return userDao.getUserByUsername(user.getUsername());
    }

    @Override
    public int changePassword(Integer id,String newpassword,String oldpassword,String confirmpassword) {
        User loginuser = getUserByID(id);
        if (SYS.checkPass(oldpassword,loginuser.getPassword()) ){
            if (newpassword.equals(confirmpassword)) {
                String hexpass= SYS.digest(newpassword);
                newpassword=hexpass;
               userDao.changePassword(id,newpassword);
            }
            else {
                throw new RuntimeException("两次输入的密码不一样！");
            }
        }else {
            throw new RuntimeException("原密码错误！");
        }




        return userDao.changePassword(id,newpassword);
    }


    @Override
    public int reloadPassword(Integer id){
        String hexpass= SYS.digest("123456");
        return userDao.reloadPassword(id,hexpass);
    }

    @Override
    public List<User> selectUsers  (Page page, String text) {
        return userDao.selectUsers(page, text);
    }

    @Override
    public int getCount(String text) {
        return userDao.getCount(text);
    }

    @Override
    public int deleteUser(int id) {
        return userDao.deleteUser(id);
    }
}
